import os
import time
from min_utilities import Bater_cpu
import config
import obj_config
from language import language


class Apps:
    """
    Класс встроенных модулей ядра голосового помощника.
    Каждая функция является не зависимой командой.
    Многие функции являются для проверки команд.
    """

    def test_tags_2(self, tags):
        print(tags)
        return "Все ОК"

    def file_manage(self):
        return obj_config.obj_file_maneg.start()

    def restart_modul(self):
        return obj_config.service_functions_obj.restart_modul()

    def json_renewal(self):
        return obj_config.service_functions_obj.json_renewal()

    def i_what(self):
        return "2022 год в Омск"

    def lenta_tags_2(self, tags_v2):
        print(tags_v2)
        mess = ""
        if "blue" in tags_v2:
            mess = mess + "255,"
        else:
            mess = mess + "0,"

        if "red" in tags_v2:
            mess = mess + "255,"
        else:
            mess = mess + "0,"
        if "grin" in tags_v2:
            mess = mess + "255;"
        else:
            mess = mess + "0;"
        if "all" in tags_v2:
            mess = "255,255,255;"
        if "no_all" in tags_v2:
            mess = "0,0,0;"

        return obj_config.obj_mc.serial_write(mess)

    def lenta_tags(self, tags):
        """
        for x in range(len(tags)):
            tags[x] = str(tags[x])
        mess = ",".join(tags)
        mess= mess+";"
        obj_config.obj_mc.serial_write(mess)
        """
        print(tags)
        # obj_config.obj_mc.serial_write()
        return "лента активирована"

    def calls_index_main(self):
        # Все программы вызов по индексу
        mess = obj_config.obj_wrapper_calls_func.calls_cmd_no_sr(
            [obj_config.all_programm_obj.call_program_index()]
        )
        obj_config.communication_obj.messege(mess)

    def all_func(self):
        return obj_config.all_programm_obj.call_program_index()

    def pc_exit(self) -> str:
        self.exit_core()
        os.system("sudo shutdown -h 10")
        self.exit_flag = True
        return language["apps=>Apps=>pc_exit"]["power_off_pc"]

    def exit_core(self) -> str:
        config.exit_flag = True
        exit(1)
        return language["apps=>Apps=>exit_core"]["exit_core_voice_helper"]

    def info_iron(self) -> str:
        # Информация о системе
        __obj = Bater_cpu()
        __bat = __obj.batar()
        __cpu = __obj.core_temp_info()
        __mess = f"{__bat} {__cpu}"
        del __obj
        del __bat
        del __cpu
        return __mess

    def protocols(self) -> list:
        list_func = []
        time.sleep(2)
        list_file = os.listdir(config.protocol_file_dir)
        index = obj_config.wrapper_decoders_obj.selection_file_decoders(list_file)
        if index != -1:
            file = open(f"{config.protocol_file_dir}/{list_file[index]}", "r")
            for str_file in file:
                if len(str_file) > 1:
                    str_file = str_file.replace("\n", "")
                    list_func.append(str_file)
                else:
                    pass
            return list_func
        else:
            return []

    def message(self, mess: str) -> None:
        obj_config.communication_obj.messege(mess)

    def del_databse(self):
        list_dir = os.listdir(f"{config.path_core}/database/")
        # list_dir = list_dir + os.listdir(f"{config.path_core}/log_error/")
        # list_dir = list_dir + os.listdir(f"{config.path_core}/old_egg_json")
        # list_dir = list_dir + os.listdir(f"{config.path_core}/egg_json")
        # list_dir = list_dir + os.listdir(f"{config.path_core}/egg")
        if len(list_dir) != 0:
            for x in range(len(list_dir)):
                list_dir[x] = f"{config.path_core}/database/{list_dir[x]}"
            str_dir = " ".join(list_dir)
            print(str_dir)
            time.sleep(3)
            os.system(f"shred -u -z -n 40 {str_dir}")
        else:
            pass

    def help(self, list_param):
        param = list_param[0]
        # print(param,2222222)
        if param == "all":
            return obj_config.info_core_obj.info_core_all()
        elif param == "cmds":
            return obj_config.info_core_obj.info_cmds()
        elif param == "collection":
            return obj_config.info_core_obj.info_collection()
        elif param == "license":
            return obj_config.info_core_obj.info_license()
        elif param == "programms":
            return obj_config.info_core_obj.info_programms()
        elif param == "verison":
            return obj_config.info_core_obj.info_add_verison()
        elif param == "author":
            return obj_config.info_core_obj.info_author()
        elif param == "basic":
            return obj_config.info_core_obj.info_basic()

    def settingsKernel(self, param):
        if param == "sett_cheek_say":
            return obj_config.obj_settings_kernel.sett_cheek_say()
        elif param == "sett_enabling_activation":
            return obj_config.obj_settings_kernel.sett_enabling_activation()

    def loop_call_func(self):
        return obj_config.wrapper_decoders_obj.loop_call_func()

    def ModulVoiceActiv(self, func):
        obj_config.communication_obj.modul_voice_activ(func)
        return "Вывод изменён"

    def call_index_cmd_name(self):
        cmd = obj_config.all_programm_obj.call_program_index_cmd_name()
        return obj_config.obj_wrapper_calls_func.calls_cmd_no_spech_recogn(cmd)

    def call_index_text_name(self):
        cmd = obj_config.all_programm_obj.call_program_index_text_name()
        return obj_config.obj_wrapper_calls_func.calls_cmd_no_spech_recogn(cmd)

    def error(self, *args, **kwargs):
        return language["import_decoder=>Import_core_apps=>__error"]["mess"]

    def func_core_ex(self, tags):
        for x in tags:
            obj_config.communication_obj.messege(x)
